//
//  YAYISettingViewController.m
//  yayi
//
//  Created by Veer on 16/3/18.
//  Copyright © 2016年 hxp. All rights reserved.
// 设置界面父类

#import "YAYISettingViewController.h"
#import "YAYISettingItemModel.h"
#import "YAYISettingCell.h"
#import "YAYISettingSectionModel.h"

@interface YAYISettingViewController ()

@end

@implementation YAYISettingViewController


- (NSMutableArray *)sectionArray
{
    if (nil == _sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0;
    
    self.tableView.contentInset = UIEdgeInsetsMake(-22, 0, 0, 0);
    self.tableView.separatorColor = YAYIColor(0xdd, 0xdd, 0xdd);
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}




#pragma mark - uitabarController代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    YAYISettingSectionModel *sectionModel = self.sectionArray[section];
    return sectionModel.itemArray.count;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    //    view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    view.frame = CGRectMake(0, 0, SCREEN_width, 10);
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出数据模型
    YAYISettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    YAYISettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    
    if (indexPath.section == 0) {
        YAYISettingCell *cell = [YAYISettingCell cellWith:tableView];
        cell.model = itemModel;
        return cell;
    }
    
    YAYISettingCell *cell = [YAYISettingCell cellWith:tableView];
    cell.model = itemModel;
    [cell setLayoutMargins:UIEdgeInsetsZero];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 取出数据模型
    YAYISettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    YAYISettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    
    // 2.操作
    if (itemModel.executeOperation) {
        itemModel.executeOperation(indexPath);
    }
    
    // 3.跳转
    if ([itemModel isKindOfClass:[YAYISettingItemModel class]]) {
//        YAYISettingItem *arrowItem = (YAYISettingItem *)item;
        if (itemModel.destVcClass) {
            UIViewController *destVc = [[itemModel.destVcClass alloc] init];
            [self.navigationController pushViewController:destVc animated:YES];
        }
    }
}

@end
